/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.rest;

import io.swagger.annotations.Api;
import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.system.domain.FlowConfig;
import me.zhengjie.modules.system.domain.FlowType;
import me.zhengjie.modules.system.domain.vo.FlowTypeQueryCriteria;
import me.zhengjie.modules.system.service.FlowConfigService;
import me.zhengjie.modules.system.domain.vo.FlowConfigQueryCriteria;
import lombok.RequiredArgsConstructor;

import java.util.ArrayList;
import java.util.List;

import me.zhengjie.modules.system.service.FlowTypeService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.zhengjie.utils.PageResult;
import org.springframework.web.bind.annotation.PostMapping;

/**
* @author pysduck
* @date 2024-07-08
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "系统：流程配置管理")
@RequestMapping("/api/flowConfig")
public class FlowConfigController {

    private final FlowConfigService flowConfigService;
    private final FlowTypeService flowTypeService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('flowConfig:list')")
    public void exportFlowConfig(HttpServletResponse response, FlowConfigQueryCriteria criteria) throws IOException {
        flowConfigService.download(flowConfigService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询系统：流程配置")
    @ApiOperation("查询系统：流程配置")
    @PreAuthorize("@el.check('flowConfig:list')")
    public ResponseEntity<PageResult<FlowConfig>> queryFlowConfig(FlowConfigQueryCriteria criteria, Page<Object> page){
        return new ResponseEntity<>(flowConfigService.queryAll(criteria,page),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增系统：流程配置")
    @ApiOperation("新增系统：流程配置")
    @PreAuthorize("@el.check('flowConfig:add')")
    public ResponseEntity<Object> createFlowConfig(@Validated @RequestBody FlowConfig resources){
        flowConfigService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改系统：流程配置")
    @ApiOperation("修改系统：流程配置")
    @PreAuthorize("@el.check('flowConfig:edit')")
    public ResponseEntity<Object> updateFlowConfig(@Validated @RequestBody FlowConfig resources){
        flowConfigService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除系统：流程配置")
    @ApiOperation("删除系统：流程配置")
    @PreAuthorize("@el.check('flowConfig:del')")
    public ResponseEntity<Object> deleteFlowConfig(@RequestBody List<Integer> ids) {
        flowConfigService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping(value = "/editProcess")
    @Log("编辑流程：设计流程")
    @ApiOperation("编辑流程：设计流程")
    @PreAuthorize("@el.check('flowConfig:edit')")
    public ResponseEntity<Object> editProcess(@RequestBody FlowConfig resources) {
        flowConfigService.editProcess(resources);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping(value = "/deployProcess")
    @Log("部署流程：部署流程")
    @ApiOperation("部署流程：部署流程")
    @PreAuthorize("@el.check('flowConfig:edit')")
    public ResponseEntity<Object> deployProcess(@RequestBody FlowConfig resources) throws Exception {
        flowConfigService.deployProcess(resources);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @AnonymousAccess
    @GetMapping(value = "/getTasks")
    public ResponseEntity<Object> getTasks(){
        return  new ResponseEntity<>(flowConfigService.getTasks("1"),HttpStatus.OK);
    }

}